<!-- HTML header for doxygen 1.8.6-->
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<title>OpenCV: samples/cpp/train_HOG.cpp</title>
<link href="../../opencv.ico" rel="shortcut icon" type="image/x-icon" />
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<script type="text/javascript" src="../../tutorial-utils.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/x-mathjax-config">
  MathJax.Hub.Config({
    extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
    jax: ["input/TeX","output/HTML-CSS"],
});
//<![CDATA[
MathJax.Hub.Config(
{
  TeX: {
      Macros: {
          matTT: [ "\\[ \\left|\\begin{array}{ccc} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{array}\\right| \\]", 9],
          fork: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ \\end{array} \\right.", 4],
          forkthree: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ \\end{array} \\right.", 6],
          forkfour: ["\\left\\{ \\begin{array}{l l} #1 & \\mbox{#2}\\\\ #3 & \\mbox{#4}\\\\ #5 & \\mbox{#6}\\\\ #7 & \\mbox{#8}\\\\ \\end{array} \\right.", 8],
          vecthree: ["\\begin{bmatrix} #1\\\\ #2\\\\ #3 \\end{bmatrix}", 3],
          vecthreethree: ["\\begin{bmatrix} #1 & #2 & #3\\\\ #4 & #5 & #6\\\\ #7 & #8 & #9 \\end{bmatrix}", 9],
          cameramatrix: ["#1 = \\begin{bmatrix} f_x & 0 & c_x\\\\ 0 & f_y & c_y\\\\ 0 & 0 & 1 \\end{bmatrix}", 1],
          distcoeffs: ["(k_1, k_2, p_1, p_2[, k_3[, k_4, k_5, k_6 [, s_1, s_2, s_3, s_4[, \\tau_x, \\tau_y]]]]) \\text{ of 4, 5, 8, 12 or 14 elements}"],
          distcoeffsfisheye: ["(k_1, k_2, k_3, k_4)"],
          hdotsfor: ["\\dots", 1],
          mathbbm: ["\\mathbb{#1}", 1],
          bordermatrix: ["\\matrix{#1}", 1]
      }
  }
}
);
//]]>
</script><script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.0/MathJax.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
<link href="../../stylesheet.css" rel="stylesheet" type="text/css"/>
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<!--#include virtual="/google-search.html"-->
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  <td id="projectlogo"><img alt="Logo" src="../../opencv-logo-small.png"/></td>
  <td style="padding-left: 0.5em;">
   <div id="projectname">OpenCV
   &#160;<span id="projectnumber">4.5.2</span>
   </div>
   <div id="projectbrief">Open Source Computer Vision</div>
  </td>
 </tr>
 </tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
<script type="text/javascript">
var searchBox = new SearchBox("searchBox", "../../search",false,'Search');
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
$(function() {
  initMenu('../../',true,false,'search.php','Search');
  $(document).ready(function() { init_search(); });
});
</script>
<div id="main-nav"></div>
</div><!-- top -->
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
     onmouseover="return searchBox.OnSearchSelectShow()"
     onmouseout="return searchBox.OnSearchSelectHide()"
     onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>

<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0" 
        name="MSearchResults" id="MSearchResults">
</iframe>
</div>

<div class="header">
  <div class="headertitle">
<div class="title">samples/cpp/train_HOG.cpp</div>  </div>
</div><!--header-->
<div class="contents">
<div class="fragment"><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="../../d1/d4f/imgproc_2include_2opencv2_2imgproc_8hpp.html">opencv2/imgproc.hpp</a>&quot;</span></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="../../d4/dd5/highgui_8hpp.html">opencv2/highgui.hpp</a>&quot;</span></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="../../d3/d29/ml_8hpp.html">opencv2/ml.hpp</a>&quot;</span></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="../../d8/da3/objdetect_8hpp.html">opencv2/objdetect.hpp</a>&quot;</span></div><div class="line"><span class="preprocessor">#include &quot;<a class="code" href="../../dc/d3d/videoio_8hpp.html">opencv2/videoio.hpp</a>&quot;</span></div><div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;time.h&gt;</span></div><div class="line"></div><div class="line"><span class="keyword">using namespace </span><a class="code" href="../../d2/d75/namespacecv.html">cv</a>;</div><div class="line"><span class="keyword">using namespace </span><a class="code" href="../../d8/df1/namespacecv_1_1ml.html">cv::ml</a>;</div><div class="line"><span class="keyword">using namespace </span><a class="code" href="../../d8/dcc/namespacestd.html">std</a>;</div><div class="line"></div><div class="line">vector&lt; float &gt; get_svm_detector( <span class="keyword">const</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga6395ca871a678020c4a31fadf7e8cc63">Ptr&lt; SVM &gt;</a>&amp; svm );</div><div class="line"><span class="keywordtype">void</span> convert_to_ml( <span class="keyword">const</span> std::vector&lt; Mat &gt; &amp; train_samples, <a name="_a0"></a><a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a>&amp; trainData );</div><div class="line"><span class="keywordtype">void</span> load_images( <span class="keyword">const</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> &amp; dirname, vector&lt; Mat &gt; &amp; img_lst, <span class="keywordtype">bool</span> showImages );</div><div class="line"><span class="keywordtype">void</span> sample_neg( <span class="keyword">const</span> vector&lt; Mat &gt; &amp; full_neg_lst, vector&lt; Mat &gt; &amp; neg_lst, <span class="keyword">const</span> <a name="_a1"></a><a class="code" href="../../d6/d50/classcv_1_1Size__.html">Size</a> &amp; <a name="a2"></a><a class="code" href="../../df/d5b/namespacecv_1_1gapi_1_1streaming.html#a0a915e69f4cc8284293e40fc9ffbf157">size</a> );</div><div class="line"><span class="keywordtype">void</span> computeHOGs( <span class="keyword">const</span> <a class="code" href="../../d6/d50/classcv_1_1Size__.html">Size</a> wsize, <span class="keyword">const</span> vector&lt; Mat &gt; &amp; img_lst, vector&lt; Mat &gt; &amp; gradient_lst, <span class="keywordtype">bool</span> use_flip );</div><div class="line"><span class="keywordtype">void</span> test_trained_detector( <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> obj_det_filename, <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> test_dir, <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> videofilename );</div><div class="line"></div><div class="line">vector&lt; float &gt; get_svm_detector( <span class="keyword">const</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga6395ca871a678020c4a31fadf7e8cc63">Ptr&lt; SVM &gt;</a>&amp; svm )</div><div class="line">{</div><div class="line">    <span class="comment">// get the support vectors</span></div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> sv = svm-&gt;getSupportVectors();</div><div class="line">    <span class="keyword">const</span> <span class="keywordtype">int</span> sv_total = sv.<a name="a3"></a><a class="code" href="../../d3/d63/classcv_1_1Mat.html#abed816466c45234254d25bc59c31245e">rows</a>;</div><div class="line">    <span class="comment">// get the decision function</span></div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> alpha, svidx;</div><div class="line">    <span class="keywordtype">double</span> rho = svm-&gt;getDecisionFunction( 0, alpha, svidx );</div><div class="line"></div><div class="line">    <a name="a4"></a><a class="code" href="../../db/de0/group__core__utils.html#gaf62bcd90f70e275191ab95136d85906b">CV_Assert</a>( alpha.<a name="a5"></a><a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa4d317d43fb0cba9c2503f3c61b866c8">total</a>() == 1 &amp;&amp; svidx.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa4d317d43fb0cba9c2503f3c61b866c8">total</a>() == 1 &amp;&amp; sv_total == 1 );</div><div class="line">    <a class="code" href="../../db/de0/group__core__utils.html#gaf62bcd90f70e275191ab95136d85906b">CV_Assert</a>( (alpha.<a name="a6"></a><a class="code" href="../../d3/d63/classcv_1_1Mat.html#af2d2652e552d7de635988f18a84b53e5">type</a>() == <a name="a7"></a><a class="code" href="../../d1/d1b/group__core__hal__interface.html#ga30a562691cc5987bc88eb7bb7a8faf2b">CV_64F</a> &amp;&amp; alpha.<a name="a8"></a><a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa5d20fc86d41d59e4d71ae93daee9726">at</a>&lt;<span class="keywordtype">double</span>&gt;(0) == 1.) ||</div><div class="line">               (alpha.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#af2d2652e552d7de635988f18a84b53e5">type</a>() == <a name="a9"></a><a class="code" href="../../d1/d1b/group__core__hal__interface.html#ga4a3def5d72b74bed31f5f8ab7676099c">CV_32F</a> &amp;&amp; alpha.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa5d20fc86d41d59e4d71ae93daee9726">at</a>&lt;<span class="keywordtype">float</span>&gt;(0) == 1.f) );</div><div class="line">    <a class="code" href="../../db/de0/group__core__utils.html#gaf62bcd90f70e275191ab95136d85906b">CV_Assert</a>( sv.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#af2d2652e552d7de635988f18a84b53e5">type</a>() == <a class="code" href="../../d1/d1b/group__core__hal__interface.html#ga4a3def5d72b74bed31f5f8ab7676099c">CV_32F</a> );</div><div class="line"></div><div class="line">    vector&lt; float &gt; hog_detector( sv.<a name="a10"></a><a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa3e5a47585c9ef6a0842556739155e3e">cols</a> + 1 );</div><div class="line">    memcpy( &amp;hog_detector[0], sv.<a name="a11"></a><a class="code" href="../../d3/d63/classcv_1_1Mat.html#a13acd320291229615ef15f96ff1ff738">ptr</a>(), sv.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa3e5a47585c9ef6a0842556739155e3e">cols</a>*<span class="keyword">sizeof</span>( hog_detector[0] ) );</div><div class="line">    hog_detector[sv.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa3e5a47585c9ef6a0842556739155e3e">cols</a>] = (float)-rho;</div><div class="line">    <span class="keywordflow">return</span> hog_detector;</div><div class="line">}</div><div class="line"></div><div class="line"><span class="comment">/*</span></div><div class="line"><span class="comment">* Convert training/testing set to be used by OpenCV Machine Learning algorithms.</span></div><div class="line"><span class="comment">* TrainData is a matrix of size (#samples x max(#cols,#rows) per samples), in 32FC1.</span></div><div class="line"><span class="comment">* Transposition of samples are made if needed.</span></div><div class="line"><span class="comment">*/</span></div><div class="line"><span class="keywordtype">void</span> convert_to_ml( <span class="keyword">const</span> vector&lt; Mat &gt; &amp; train_samples, <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a>&amp; trainData )</div><div class="line">{</div><div class="line">    <span class="comment">//--Convert data</span></div><div class="line">    <span class="keyword">const</span> <span class="keywordtype">int</span> rows = (int)train_samples.size();</div><div class="line">    <span class="keyword">const</span> <span class="keywordtype">int</span> cols = (int)<a name="a12"></a><a class="code" href="../../d7/dcc/group__core__utils__softfloat.html#ga78f988f6cfa6223610298cbd4f86ec66">std::max</a>( train_samples[0].cols, train_samples[0].rows );</div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> tmp( 1, cols, <a name="a13"></a><a class="code" href="../../d1/d1b/group__core__hal__interface.html#ga32ec76240e43e4c9c7b2e2785180a7e6">CV_32FC1</a> ); <span class="comment">//&lt; used for transposition if needed</span></div><div class="line">    trainData = <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a>( rows, cols, <a class="code" href="../../d1/d1b/group__core__hal__interface.html#ga32ec76240e43e4c9c7b2e2785180a7e6">CV_32FC1</a> );</div><div class="line"></div><div class="line">    <span class="keywordflow">for</span>( <span class="keywordtype">size_t</span> i = 0 ; i &lt; train_samples.size(); ++i )</div><div class="line">    {</div><div class="line">        <a class="code" href="../../db/de0/group__core__utils.html#gaf62bcd90f70e275191ab95136d85906b">CV_Assert</a>( train_samples[i].cols == 1 || train_samples[i].rows == 1 );</div><div class="line"></div><div class="line">        <span class="keywordflow">if</span>( train_samples[i].cols == 1 )</div><div class="line">        {</div><div class="line">            <a name="a14"></a><a class="code" href="../../d2/de8/group__core__array.html#ga46630ed6c0ea6254a35f447289bd7404">transpose</a>( train_samples[i], tmp );</div><div class="line">            tmp.copyTo( trainData.<a name="a15"></a><a class="code" href="../../d3/d63/classcv_1_1Mat.html#a4b22e1c23af7a7f2eef8fa478cfa7434">row</a>( (<span class="keywordtype">int</span>)i ) );</div><div class="line">        }</div><div class="line">        <span class="keywordflow">else</span> <span class="keywordflow">if</span>( train_samples[i].rows == 1 )</div><div class="line">        {</div><div class="line">            train_samples[i].copyTo( trainData.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#a4b22e1c23af7a7f2eef8fa478cfa7434">row</a>( (<span class="keywordtype">int</span>)i ) );</div><div class="line">        }</div><div class="line">    }</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">void</span> load_images( <span class="keyword">const</span> <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> &amp; dirname, vector&lt; Mat &gt; &amp; img_lst, <span class="keywordtype">bool</span> showImages = <span class="keyword">false</span> )</div><div class="line">{</div><div class="line">    vector&lt; String &gt; files;</div><div class="line">    <a name="a16"></a><a class="code" href="../../db/de0/group__core__utils.html#gaf91b7f383218e2d3f760939140942297">glob</a>( dirname, files );</div><div class="line"></div><div class="line">    <span class="keywordflow">for</span> ( <span class="keywordtype">size_t</span> i = 0; i &lt; files.size(); ++i )</div><div class="line">    {</div><div class="line">        <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> img = <a name="a17"></a><a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">imread</a>( files[i] ); <span class="comment">// load the image</span></div><div class="line">        <span class="keywordflow">if</span> ( img.<a name="a18"></a><a class="code" href="../../d3/d63/classcv_1_1Mat.html#abbec3525a852e77998aba034813fded4">empty</a>() )</div><div class="line">        {</div><div class="line">            cout &lt;&lt; files[i] &lt;&lt; <span class="stringliteral">&quot; is invalid!&quot;</span> &lt;&lt; endl; <span class="comment">// invalid image, skip it.</span></div><div class="line">            <span class="keywordflow">continue</span>;</div><div class="line">        }</div><div class="line"></div><div class="line">        <span class="keywordflow">if</span> ( showImages )</div><div class="line">        {</div><div class="line">            <a name="a19"></a><a class="code" href="../../d7/dfc/group__highgui.html#ga453d42fe4cb60e5723281a89973ee563">imshow</a>( <span class="stringliteral">&quot;image&quot;</span>, img );</div><div class="line">            <a name="a20"></a><a class="code" href="../../d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7">waitKey</a>( 1 );</div><div class="line">        }</div><div class="line">        img_lst.push_back( img );</div><div class="line">    }</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">void</span> sample_neg( <span class="keyword">const</span> vector&lt; Mat &gt; &amp; full_neg_lst, vector&lt; Mat &gt; &amp; neg_lst, <span class="keyword">const</span> <a class="code" href="../../d6/d50/classcv_1_1Size__.html">Size</a> &amp; <a class="code" href="../../df/d5b/namespacecv_1_1gapi_1_1streaming.html#a0a915e69f4cc8284293e40fc9ffbf157">size</a> )</div><div class="line">{</div><div class="line">    <a name="_a21"></a><a class="code" href="../../d2/d44/classcv_1_1Rect__.html">Rect</a> box;</div><div class="line">    box.<a name="a22"></a><a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6c16a3bce912faa4fe5be42d7f1b53fe">width</a> = size.<a name="a23"></a><a class="code" href="../../d6/d50/classcv_1_1Size__.html#abfe0367b32c407ddccf5ddf92667c73d">width</a>;</div><div class="line">    box.<a name="a24"></a><a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6fed06513cedd76652389e38c7b1222e">height</a> = size.<a name="a25"></a><a class="code" href="../../d6/d50/classcv_1_1Size__.html#a1d289dce6b5d8006a54f3ee0259fc545">height</a>;</div><div class="line"></div><div class="line">    srand( (<span class="keywordtype">unsigned</span> <span class="keywordtype">int</span>)time( NULL ) );</div><div class="line"></div><div class="line">    <span class="keywordflow">for</span> ( <span class="keywordtype">size_t</span> i = 0; i &lt; full_neg_lst.size(); i++ )</div><div class="line">        <span class="keywordflow">if</span> ( full_neg_lst[i].cols &gt; box.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6c16a3bce912faa4fe5be42d7f1b53fe">width</a> &amp;&amp; full_neg_lst[i].rows &gt; box.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6fed06513cedd76652389e38c7b1222e">height</a> )</div><div class="line">        {</div><div class="line">            box.<a name="a26"></a><a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a2cadfdc3b4b7dbf8085622b27e044572">x</a> = rand() % ( full_neg_lst[i].cols - box.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6c16a3bce912faa4fe5be42d7f1b53fe">width</a> );</div><div class="line">            box.<a name="a27"></a><a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6a4860e984df1752623b6ce2a8bde73a">y</a> = rand() % ( full_neg_lst[i].rows - box.<a class="code" href="../../d2/d44/classcv_1_1Rect__.html#a6fed06513cedd76652389e38c7b1222e">height</a> );</div><div class="line">            <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> roi = full_neg_lst[i]( box );</div><div class="line">            neg_lst.push_back( roi.<a name="a28"></a><a class="code" href="../../d3/d63/classcv_1_1Mat.html#adff2ea98da45eae0833e73582dd4a660">clone</a>() );</div><div class="line">        }</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">void</span> computeHOGs( <span class="keyword">const</span> <a class="code" href="../../d6/d50/classcv_1_1Size__.html">Size</a> wsize, <span class="keyword">const</span> vector&lt; Mat &gt; &amp; img_lst, vector&lt; Mat &gt; &amp; gradient_lst, <span class="keywordtype">bool</span> use_flip )</div><div class="line">{</div><div class="line">    <a name="_a29"></a><a class="code" href="../../d5/d33/structcv_1_1HOGDescriptor.html">HOGDescriptor</a> hog;</div><div class="line">    hog.<a name="a30"></a><a class="code" href="../../d5/d33/structcv_1_1HOGDescriptor.html#a75329167dfdacde41b488e091dd555d1">winSize</a> = wsize;</div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> gray;</div><div class="line">    vector&lt; float &gt; descriptors;</div><div class="line"></div><div class="line">    <span class="keywordflow">for</span>( <span class="keywordtype">size_t</span> i = 0 ; i &lt; img_lst.size(); i++ )</div><div class="line">    {</div><div class="line">        <span class="keywordflow">if</span> ( img_lst[i].cols &gt;= wsize.<a class="code" href="../../d6/d50/classcv_1_1Size__.html#abfe0367b32c407ddccf5ddf92667c73d">width</a> &amp;&amp; img_lst[i].rows &gt;= wsize.<a class="code" href="../../d6/d50/classcv_1_1Size__.html#a1d289dce6b5d8006a54f3ee0259fc545">height</a> )</div><div class="line">        {</div><div class="line">            <a class="code" href="../../d2/d44/classcv_1_1Rect__.html">Rect</a> r = <a name="a31"></a><a class="code" href="../../dc/d84/group__core__basic.html#ga11d95de507098e90bad732b9345402e8">Rect</a>(( img_lst[i].cols - wsize.<a class="code" href="../../d6/d50/classcv_1_1Size__.html#abfe0367b32c407ddccf5ddf92667c73d">width</a> ) / 2,</div><div class="line">                          ( img_lst[i].rows - wsize.<a class="code" href="../../d6/d50/classcv_1_1Size__.html#a1d289dce6b5d8006a54f3ee0259fc545">height</a> ) / 2,</div><div class="line">                          wsize.<a class="code" href="../../d6/d50/classcv_1_1Size__.html#abfe0367b32c407ddccf5ddf92667c73d">width</a>,</div><div class="line">                          wsize.<a class="code" href="../../d6/d50/classcv_1_1Size__.html#a1d289dce6b5d8006a54f3ee0259fc545">height</a>);</div><div class="line">            <a name="a32"></a><a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#ga397ae87e1288a81d2363b61574eb8cab">cvtColor</a>( img_lst[i](r), gray, <a name="a33"></a><a class="code" href="../../d8/d01/group__imgproc__color__conversions.html#gga4e0972be5de079fed4e3a10e24ef5ef0a353a4b8db9040165db4dacb5bcefb6ea">COLOR_BGR2GRAY</a> );</div><div class="line">            hog.<a name="a34"></a><a class="code" href="../../d5/d33/structcv_1_1HOGDescriptor.html#a38cd712cd5a6d9ed0344731fcd121e8b">compute</a>( gray, descriptors, <a name="a35"></a><a class="code" href="../../dc/d84/group__core__basic.html#ga346f563897249351a34549137c8532a0">Size</a>( 8, 8 ), <a class="code" href="../../dc/d84/group__core__basic.html#ga346f563897249351a34549137c8532a0">Size</a>( 0, 0 ) );</div><div class="line">            gradient_lst.push_back( <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a>( descriptors ).clone() );</div><div class="line">            <span class="keywordflow">if</span> ( use_flip )</div><div class="line">            {</div><div class="line">                <a name="a36"></a><a class="code" href="../../d2/de8/group__core__array.html#gaca7be533e3dac7feb70fc60635adf441">flip</a>( gray, gray, 1 );</div><div class="line">                hog.<a class="code" href="../../d5/d33/structcv_1_1HOGDescriptor.html#a38cd712cd5a6d9ed0344731fcd121e8b">compute</a>( gray, descriptors, <a class="code" href="../../dc/d84/group__core__basic.html#ga346f563897249351a34549137c8532a0">Size</a>( 8, 8 ), <a class="code" href="../../dc/d84/group__core__basic.html#ga346f563897249351a34549137c8532a0">Size</a>( 0, 0 ) );</div><div class="line">                gradient_lst.push_back( <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a>( descriptors ).clone() );</div><div class="line">            }</div><div class="line">        }</div><div class="line">    }</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">void</span> test_trained_detector( <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> obj_det_filename, <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> test_dir, <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> videofilename )</div><div class="line">{</div><div class="line">    cout &lt;&lt; <span class="stringliteral">&quot;Testing trained detector...&quot;</span> &lt;&lt; endl;</div><div class="line">    <a class="code" href="../../d5/d33/structcv_1_1HOGDescriptor.html">HOGDescriptor</a> hog;</div><div class="line">    hog.<a name="a37"></a><a class="code" href="../../d5/d33/structcv_1_1HOGDescriptor.html#a957f55d36ba2f1a2ae47883c11f4b730">load</a>( obj_det_filename );</div><div class="line"></div><div class="line">    vector&lt; String &gt; files;</div><div class="line">    <a class="code" href="../../db/de0/group__core__utils.html#gaf91b7f383218e2d3f760939140942297">glob</a>( test_dir, files );</div><div class="line"></div><div class="line">    <span class="keywordtype">int</span> delay = 0;</div><div class="line">    <a name="_a38"></a><a class="code" href="../../d8/dfe/classcv_1_1VideoCapture.html">VideoCapture</a> cap;</div><div class="line"></div><div class="line">    <span class="keywordflow">if</span> ( videofilename != <span class="stringliteral">&quot;&quot;</span> )</div><div class="line">    {</div><div class="line">        <span class="keywordflow">if</span> ( videofilename.size() == 1 &amp;&amp; isdigit( videofilename[0] ) )</div><div class="line">            cap.<a name="a39"></a><a class="code" href="../../d8/dfe/classcv_1_1VideoCapture.html#a614a1702e15f42ede5100014ce7f48ed">open</a>( videofilename[0] - <span class="charliteral">&#39;0&#39;</span> );</div><div class="line">        <span class="keywordflow">else</span></div><div class="line">            cap.<a class="code" href="../../d8/dfe/classcv_1_1VideoCapture.html#a614a1702e15f42ede5100014ce7f48ed">open</a>( videofilename );</div><div class="line">    }</div><div class="line"></div><div class="line">    obj_det_filename = <span class="stringliteral">&quot;testing &quot;</span> + obj_det_filename;</div><div class="line">    <a name="a40"></a><a class="code" href="../../d7/dfc/group__highgui.html#ga5afdf8410934fd099df85c75b2e0888b">namedWindow</a>( obj_det_filename, <a name="a41"></a><a class="code" href="../../d0/d90/group__highgui__window__flags.html#ggabf7d2c5625bc59ac130287f925557ac3a29e45c5af696f73ce5e153601e5ca0f1">WINDOW_NORMAL</a> );</div><div class="line"></div><div class="line">    <span class="keywordflow">for</span>( <span class="keywordtype">size_t</span> i=0;; i++ )</div><div class="line">    {</div><div class="line">        <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> img;</div><div class="line"></div><div class="line">        <span class="keywordflow">if</span> ( cap.<a name="a42"></a><a class="code" href="../../d8/dfe/classcv_1_1VideoCapture.html#a9d2ca36789e7fcfe7a7be3b328038585">isOpened</a>() )</div><div class="line">        {</div><div class="line">            cap &gt;&gt; img;</div><div class="line">            delay = 1;</div><div class="line">        }</div><div class="line">        <span class="keywordflow">else</span> <span class="keywordflow">if</span>( i &lt; files.size() )</div><div class="line">        {</div><div class="line">            img = <a class="code" href="../../d4/da8/group__imgcodecs.html#ga288b8b3da0892bd651fce07b3bbd3a56">imread</a>( files[i] );</div><div class="line">        }</div><div class="line"></div><div class="line">        <span class="keywordflow">if</span> ( img.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#abbec3525a852e77998aba034813fded4">empty</a>() )</div><div class="line">        {</div><div class="line">            <span class="keywordflow">return</span>;</div><div class="line">        }</div><div class="line"></div><div class="line">        vector&lt; Rect &gt; detections;</div><div class="line">        vector&lt; double &gt; foundWeights;</div><div class="line"></div><div class="line">        hog.<a name="a43"></a><a class="code" href="../../d5/d33/structcv_1_1HOGDescriptor.html#a91e56a2c317392e50fbaa2f5dc78d30b">detectMultiScale</a>( img, detections, foundWeights );</div><div class="line">        <span class="keywordflow">for</span> ( <span class="keywordtype">size_t</span> j = 0; j &lt; detections.size(); j++ )</div><div class="line">        {</div><div class="line">            <a name="_a44"></a><a class="code" href="../../d1/da0/classcv_1_1Scalar__.html">Scalar</a> color = <a name="a45"></a><a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>( 0, foundWeights[j] * foundWeights[j] * 200, 0 );</div><div class="line">            <a name="a46"></a><a class="code" href="../../d6/d6e/group__imgproc__draw.html#ga07d2f74cadcf8e305e810ce8eed13bc9">rectangle</a>( img, detections[j], color, img.<a class="code" href="../../d3/d63/classcv_1_1Mat.html#aa3e5a47585c9ef6a0842556739155e3e">cols</a> / 400 + 1 );</div><div class="line">        }</div><div class="line"></div><div class="line">        <a class="code" href="../../d7/dfc/group__highgui.html#ga453d42fe4cb60e5723281a89973ee563">imshow</a>( obj_det_filename, img );</div><div class="line"></div><div class="line">        <span class="keywordflow">if</span>( <a class="code" href="../../d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7">waitKey</a>( delay ) == 27 )</div><div class="line">        {</div><div class="line">            <span class="keywordflow">return</span>;</div><div class="line">        }</div><div class="line">    }</div><div class="line">}</div><div class="line"></div><div class="line"><span class="keywordtype">int</span> main( <span class="keywordtype">int</span> argc, <span class="keywordtype">char</span>** argv )</div><div class="line">{</div><div class="line">    <span class="keyword">const</span> <span class="keywordtype">char</span>* keys =</div><div class="line">    {</div><div class="line">        <span class="stringliteral">&quot;{help h|     | show help message}&quot;</span></div><div class="line">        <span class="stringliteral">&quot;{pd    |     | path of directory contains positive images}&quot;</span></div><div class="line">        <span class="stringliteral">&quot;{nd    |     | path of directory contains negative images}&quot;</span></div><div class="line">        <span class="stringliteral">&quot;{td    |     | path of directory contains test images}&quot;</span></div><div class="line">        <span class="stringliteral">&quot;{tv    |     | test video file name}&quot;</span></div><div class="line">        <span class="stringliteral">&quot;{dw    |     | width of the detector}&quot;</span></div><div class="line">        <span class="stringliteral">&quot;{dh    |     | height of the detector}&quot;</span></div><div class="line">        <span class="stringliteral">&quot;{f     |false| indicates if the program will generate and use mirrored samples or not}&quot;</span></div><div class="line">        <span class="stringliteral">&quot;{d     |false| train twice}&quot;</span></div><div class="line">        <span class="stringliteral">&quot;{t     |false| test a trained detector}&quot;</span></div><div class="line">        <span class="stringliteral">&quot;{v     |false| visualize training steps}&quot;</span></div><div class="line">        <span class="stringliteral">&quot;{fn    |my_detector.yml| file name of trained SVM}&quot;</span></div><div class="line">    };</div><div class="line"></div><div class="line">    <a name="_a47"></a><a class="code" href="../../d0/d2e/classcv_1_1CommandLineParser.html">CommandLineParser</a> parser( argc, argv, keys );</div><div class="line"></div><div class="line">    <span class="keywordflow">if</span> ( parser.has( <span class="stringliteral">&quot;help&quot;</span> ) )</div><div class="line">    {</div><div class="line">        parser.printMessage();</div><div class="line">        exit( 0 );</div><div class="line">    }</div><div class="line"></div><div class="line">    <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> pos_dir = parser.get&lt; <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> &gt;( <span class="stringliteral">&quot;pd&quot;</span> );</div><div class="line">    <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> neg_dir = parser.get&lt; <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> &gt;( <span class="stringliteral">&quot;nd&quot;</span> );</div><div class="line">    <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> test_dir = parser.get&lt; <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> &gt;( <span class="stringliteral">&quot;td&quot;</span> );</div><div class="line">    <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> obj_det_filename = parser.get&lt; <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> &gt;( <span class="stringliteral">&quot;fn&quot;</span> );</div><div class="line">    <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> videofilename = parser.get&lt; <a class="code" href="../../dc/d84/group__core__basic.html#ga1f6634802eeadfd7245bc75cf3e216c2">String</a> &gt;( <span class="stringliteral">&quot;tv&quot;</span> );</div><div class="line">    <span class="keywordtype">int</span> detector_width = parser.get&lt; <span class="keywordtype">int</span> &gt;( <span class="stringliteral">&quot;dw&quot;</span> );</div><div class="line">    <span class="keywordtype">int</span> detector_height = parser.get&lt; <span class="keywordtype">int</span> &gt;( <span class="stringliteral">&quot;dh&quot;</span> );</div><div class="line">    <span class="keywordtype">bool</span> test_detector = parser.get&lt; <span class="keywordtype">bool</span> &gt;( <span class="stringliteral">&quot;t&quot;</span> );</div><div class="line">    <span class="keywordtype">bool</span> train_twice = parser.get&lt; <span class="keywordtype">bool</span> &gt;( <span class="stringliteral">&quot;d&quot;</span> );</div><div class="line">    <span class="keywordtype">bool</span> visualization = parser.get&lt; <span class="keywordtype">bool</span> &gt;( <span class="stringliteral">&quot;v&quot;</span> );</div><div class="line">    <span class="keywordtype">bool</span> flip_samples = parser.get&lt; <span class="keywordtype">bool</span> &gt;( <span class="stringliteral">&quot;f&quot;</span> );</div><div class="line"></div><div class="line">    <span class="keywordflow">if</span> ( test_detector )</div><div class="line">    {</div><div class="line">        test_trained_detector( obj_det_filename, test_dir, videofilename );</div><div class="line">        exit( 0 );</div><div class="line">    }</div><div class="line"></div><div class="line">    <span class="keywordflow">if</span>( pos_dir.empty() || neg_dir.empty() )</div><div class="line">    {</div><div class="line">        parser.printMessage();</div><div class="line">        cout &lt;&lt; <span class="stringliteral">&quot;Wrong number of parameters.\n\n&quot;</span></div><div class="line">             &lt;&lt; <span class="stringliteral">&quot;Example command line:\n&quot;</span> &lt;&lt; argv[0] &lt;&lt; <span class="stringliteral">&quot; -dw=64 -dh=128 -pd=/INRIAPerson/96X160H96/Train/pos -nd=/INRIAPerson/neg -td=/INRIAPerson/Test/pos -fn=HOGpedestrian64x128.xml -d\n&quot;</span></div><div class="line">             &lt;&lt; <span class="stringliteral">&quot;\nExample command line for testing trained detector:\n&quot;</span> &lt;&lt; argv[0] &lt;&lt; <span class="stringliteral">&quot; -t -fn=HOGpedestrian64x128.xml -td=/INRIAPerson/Test/pos&quot;</span>;</div><div class="line">        exit( 1 );</div><div class="line">    }</div><div class="line"></div><div class="line">    vector&lt; Mat &gt; pos_lst, full_neg_lst, neg_lst, gradient_lst;</div><div class="line">    vector&lt; int &gt; labels;</div><div class="line"></div><div class="line">    clog &lt;&lt; <span class="stringliteral">&quot;Positive images are being loaded...&quot;</span> ;</div><div class="line">    load_images( pos_dir, pos_lst, visualization );</div><div class="line">    <span class="keywordflow">if</span> ( pos_lst.size() &gt; 0 )</div><div class="line">    {</div><div class="line">        clog &lt;&lt; <span class="stringliteral">&quot;...[done] &quot;</span> &lt;&lt; pos_lst.size() &lt;&lt; <span class="stringliteral">&quot; files.&quot;</span> &lt;&lt; endl;</div><div class="line">    }</div><div class="line">    <span class="keywordflow">else</span></div><div class="line">    {</div><div class="line">        clog &lt;&lt; <span class="stringliteral">&quot;no image in &quot;</span> &lt;&lt; pos_dir &lt;&lt;endl;</div><div class="line">        <span class="keywordflow">return</span> 1;</div><div class="line">    }</div><div class="line"></div><div class="line">    <a class="code" href="../../d6/d50/classcv_1_1Size__.html">Size</a> pos_image_size = pos_lst[0].size();</div><div class="line"></div><div class="line">    <span class="keywordflow">if</span> ( detector_width &amp;&amp; detector_height )</div><div class="line">    {</div><div class="line">        pos_image_size = <a class="code" href="../../dc/d84/group__core__basic.html#ga346f563897249351a34549137c8532a0">Size</a>( detector_width, detector_height );</div><div class="line">    }</div><div class="line">    <span class="keywordflow">else</span></div><div class="line">    {</div><div class="line">        <span class="keywordflow">for</span> ( <span class="keywordtype">size_t</span> i = 0; i &lt; pos_lst.size(); ++i )</div><div class="line">        {</div><div class="line">            <span class="keywordflow">if</span>( pos_lst[i].<a class="code" href="../../df/d5b/namespacecv_1_1gapi_1_1streaming.html#a0a915e69f4cc8284293e40fc9ffbf157">size</a>() != pos_image_size )</div><div class="line">            {</div><div class="line">                cout &lt;&lt; <span class="stringliteral">&quot;All positive images should be same size!&quot;</span> &lt;&lt; endl;</div><div class="line">                exit( 1 );</div><div class="line">            }</div><div class="line">        }</div><div class="line">        pos_image_size = pos_image_size / 8 * 8;</div><div class="line">    }</div><div class="line"></div><div class="line">    clog &lt;&lt; <span class="stringliteral">&quot;Negative images are being loaded...&quot;</span>;</div><div class="line">    load_images( neg_dir, full_neg_lst, visualization );</div><div class="line">    clog &lt;&lt; <span class="stringliteral">&quot;...[done] &quot;</span> &lt;&lt; full_neg_lst.size() &lt;&lt; <span class="stringliteral">&quot; files.&quot;</span> &lt;&lt; endl;</div><div class="line"></div><div class="line">    clog &lt;&lt; <span class="stringliteral">&quot;Negative images are being processed...&quot;</span>;</div><div class="line">    sample_neg( full_neg_lst, neg_lst, pos_image_size );</div><div class="line">    clog &lt;&lt; <span class="stringliteral">&quot;...[done] &quot;</span> &lt;&lt; neg_lst.size() &lt;&lt; <span class="stringliteral">&quot; files.&quot;</span> &lt;&lt; endl;</div><div class="line"></div><div class="line">    clog &lt;&lt; <span class="stringliteral">&quot;Histogram of Gradients are being calculated for positive images...&quot;</span>;</div><div class="line">    computeHOGs( pos_image_size, pos_lst, gradient_lst, flip_samples );</div><div class="line">    <span class="keywordtype">size_t</span> positive_count = gradient_lst.size();</div><div class="line">    labels.assign( positive_count, +1 );</div><div class="line">    clog &lt;&lt; <span class="stringliteral">&quot;...[done] ( positive images count : &quot;</span> &lt;&lt; positive_count &lt;&lt; <span class="stringliteral">&quot; )&quot;</span> &lt;&lt; endl;</div><div class="line"></div><div class="line">    clog &lt;&lt; <span class="stringliteral">&quot;Histogram of Gradients are being calculated for negative images...&quot;</span>;</div><div class="line">    computeHOGs( pos_image_size, neg_lst, gradient_lst, flip_samples );</div><div class="line">    <span class="keywordtype">size_t</span> negative_count = gradient_lst.size() - positive_count;</div><div class="line">    labels.insert( labels.end(), negative_count, -1 );</div><div class="line">    <a class="code" href="../../db/de0/group__core__utils.html#gaf62bcd90f70e275191ab95136d85906b">CV_Assert</a>( positive_count &lt; labels.size() );</div><div class="line">    clog &lt;&lt; <span class="stringliteral">&quot;...[done] ( negative images count : &quot;</span> &lt;&lt; negative_count &lt;&lt; <span class="stringliteral">&quot; )&quot;</span> &lt;&lt; endl;</div><div class="line"></div><div class="line">    <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> train_data;</div><div class="line">    convert_to_ml( gradient_lst, train_data );</div><div class="line"></div><div class="line">    clog &lt;&lt; <span class="stringliteral">&quot;Training SVM...&quot;</span>;</div><div class="line">    <a class="code" href="../../dc/d84/group__core__basic.html#ga6395ca871a678020c4a31fadf7e8cc63">Ptr&lt; SVM &gt;</a> svm = <a name="a48"></a><a class="code" href="../../d1/d2d/classcv_1_1ml_1_1SVM.html#a2fe8b5bf897c34b8e911397b42e2cb44">SVM::create</a>();</div><div class="line">    <span class="comment">/* Default values to train SVM */</span></div><div class="line">    svm-&gt;setCoef0( 0.0 );</div><div class="line">    svm-&gt;setDegree( 3 );</div><div class="line">    svm-&gt;setTermCriteria( <a name="_a49"></a><a class="code" href="../../d9/d5d/classcv_1_1TermCriteria.html">TermCriteria</a>(<a name="a50"></a><a class="code" href="../../d9/d5d/classcv_1_1TermCriteria.html#a56fecdc291ccaba8aad27d67ccf72c57a56ca2bc5cd06345060a1c1c66a8fc06e">TermCriteria::MAX_ITER</a> + <a name="a51"></a><a class="code" href="../../d9/d5d/classcv_1_1TermCriteria.html#a56fecdc291ccaba8aad27d67ccf72c57a857609e73e7028e638d2ea649f3b45d5">TermCriteria::EPS</a>, 1000, 1e-3 ) );</div><div class="line">    svm-&gt;setGamma( 0 );</div><div class="line">    svm-&gt;setKernel( <a name="a52"></a><a class="code" href="../../d1/d2d/classcv_1_1ml_1_1SVM.html#aad7f1aaccced3c33bb256640910a0e56ab92a19ab0c193735c3fd71f938dd87e7">SVM::LINEAR</a> );</div><div class="line">    svm-&gt;setNu( 0.5 );</div><div class="line">    svm-&gt;setP( 0.1 ); <span class="comment">// for EPSILON_SVR, epsilon in loss function?</span></div><div class="line">    svm-&gt;setC( 0.01 ); <span class="comment">// From paper, soft classifier</span></div><div class="line">    svm-&gt;setType( <a name="a53"></a><a class="code" href="../../d1/d2d/classcv_1_1ml_1_1SVM.html#ab4b93a4c42bbe213ffd9fb3832c6c44fac6fd17721f6a7b5c10f3ae48b78ed944">SVM::EPS_SVR</a> ); <span class="comment">// C_SVC; // EPSILON_SVR; // may be also NU_SVR; // do regression task</span></div><div class="line">    svm-&gt;train( train_data, <a name="a54"></a><a class="code" href="../../dd/ded/group__ml.html#gga9c57a2b823008dda53d2c7f7059a8710ab8565ac2eb79152a4e3c80b0e9c9fd4c">ROW_SAMPLE</a>, labels );</div><div class="line">    clog &lt;&lt; <span class="stringliteral">&quot;...[done]&quot;</span> &lt;&lt; endl;</div><div class="line"></div><div class="line">    <span class="keywordflow">if</span> ( train_twice )</div><div class="line">    {</div><div class="line">        clog &lt;&lt; <span class="stringliteral">&quot;Testing trained detector on negative images. This might take a few minutes...&quot;</span>;</div><div class="line">        <a class="code" href="../../d5/d33/structcv_1_1HOGDescriptor.html">HOGDescriptor</a> my_hog;</div><div class="line">        my_hog.<a class="code" href="../../d5/d33/structcv_1_1HOGDescriptor.html#a75329167dfdacde41b488e091dd555d1">winSize</a> = pos_image_size;</div><div class="line"></div><div class="line">        <span class="comment">// Set the trained svm to my_hog</span></div><div class="line">        my_hog.<a name="a55"></a><a class="code" href="../../d5/d33/structcv_1_1HOGDescriptor.html#a6de5ac55631eed51e36278cde3a2c159">setSVMDetector</a>( get_svm_detector( svm ) );</div><div class="line"></div><div class="line">        vector&lt; Rect &gt; detections;</div><div class="line">        vector&lt; double &gt; foundWeights;</div><div class="line"></div><div class="line">        <span class="keywordflow">for</span> ( <span class="keywordtype">size_t</span> i = 0; i &lt; full_neg_lst.size(); i++ )</div><div class="line">        {</div><div class="line">            <span class="keywordflow">if</span> ( full_neg_lst[i].cols &gt;= pos_image_size.<a class="code" href="../../d6/d50/classcv_1_1Size__.html#abfe0367b32c407ddccf5ddf92667c73d">width</a> &amp;&amp; full_neg_lst[i].rows &gt;= pos_image_size.<a class="code" href="../../d6/d50/classcv_1_1Size__.html#a1d289dce6b5d8006a54f3ee0259fc545">height</a> )</div><div class="line">                my_hog.<a class="code" href="../../d5/d33/structcv_1_1HOGDescriptor.html#a91e56a2c317392e50fbaa2f5dc78d30b">detectMultiScale</a>( full_neg_lst[i], detections, foundWeights );</div><div class="line">            <span class="keywordflow">else</span></div><div class="line">                detections.clear();</div><div class="line"></div><div class="line">            <span class="keywordflow">for</span> ( <span class="keywordtype">size_t</span> j = 0; j &lt; detections.size(); j++ )</div><div class="line">            {</div><div class="line">                <a class="code" href="../../d3/d63/classcv_1_1Mat.html">Mat</a> detection = full_neg_lst[i]( detections[j] ).clone();</div><div class="line">                <a name="a56"></a><a class="code" href="../../da/d54/group__imgproc__transform.html#ga47a974309e9102f5f08231edc7e7529d">resize</a>( detection, detection, pos_image_size, 0, 0, <a name="a57"></a><a class="code" href="../../da/d54/group__imgproc__transform.html#gga5bb5a1fea74ea38e1a5445ca803ff121ac00f4a8155563cdc23437fc0959da935">INTER_LINEAR_EXACT</a>);</div><div class="line">                neg_lst.push_back( detection );</div><div class="line">            }</div><div class="line"></div><div class="line">            <span class="keywordflow">if</span> ( visualization )</div><div class="line">            {</div><div class="line">                <span class="keywordflow">for</span> ( <span class="keywordtype">size_t</span> j = 0; j &lt; detections.size(); j++ )</div><div class="line">                {</div><div class="line">                    <a class="code" href="../../d6/d6e/group__imgproc__draw.html#ga07d2f74cadcf8e305e810ce8eed13bc9">rectangle</a>( full_neg_lst[i], detections[j], <a class="code" href="../../dc/d84/group__core__basic.html#ga599fe92e910c027be274233eccad7beb">Scalar</a>( 0, 255, 0 ), 2 );</div><div class="line">                }</div><div class="line">                <a class="code" href="../../d7/dfc/group__highgui.html#ga453d42fe4cb60e5723281a89973ee563">imshow</a>( <span class="stringliteral">&quot;testing trained detector on negative images&quot;</span>, full_neg_lst[i] );</div><div class="line">                <a class="code" href="../../d7/dfc/group__highgui.html#ga5628525ad33f52eab17feebcfba38bd7">waitKey</a>( 5 );</div><div class="line">            }</div><div class="line">        }</div><div class="line">        clog &lt;&lt; <span class="stringliteral">&quot;...[done]&quot;</span> &lt;&lt; endl;</div><div class="line"></div><div class="line">        gradient_lst.clear();</div><div class="line">        clog &lt;&lt; <span class="stringliteral">&quot;Histogram of Gradients are being calculated for positive images...&quot;</span>;</div><div class="line">        computeHOGs( pos_image_size, pos_lst, gradient_lst, flip_samples );</div><div class="line">        positive_count = gradient_lst.size();</div><div class="line">        clog &lt;&lt; <span class="stringliteral">&quot;...[done] ( positive count : &quot;</span> &lt;&lt; positive_count &lt;&lt; <span class="stringliteral">&quot; )&quot;</span> &lt;&lt; endl;</div><div class="line"></div><div class="line">        clog &lt;&lt; <span class="stringliteral">&quot;Histogram of Gradients are being calculated for negative images...&quot;</span>;</div><div class="line">        computeHOGs( pos_image_size, neg_lst, gradient_lst, flip_samples );</div><div class="line">        negative_count = gradient_lst.size() - positive_count;</div><div class="line">        clog &lt;&lt; <span class="stringliteral">&quot;...[done] ( negative count : &quot;</span> &lt;&lt; negative_count &lt;&lt; <span class="stringliteral">&quot; )&quot;</span> &lt;&lt; endl;</div><div class="line"></div><div class="line">        labels.clear();</div><div class="line">        labels.assign(positive_count, +1);</div><div class="line">        labels.insert(labels.end(), negative_count, -1);</div><div class="line"></div><div class="line">        clog &lt;&lt; <span class="stringliteral">&quot;Training SVM again...&quot;</span>;</div><div class="line">        convert_to_ml( gradient_lst, train_data );</div><div class="line">        svm-&gt;train( train_data, <a class="code" href="../../dd/ded/group__ml.html#gga9c57a2b823008dda53d2c7f7059a8710ab8565ac2eb79152a4e3c80b0e9c9fd4c">ROW_SAMPLE</a>, labels );</div><div class="line">        clog &lt;&lt; <span class="stringliteral">&quot;...[done]&quot;</span> &lt;&lt; endl;</div><div class="line">    }</div><div class="line"></div><div class="line">    <a class="code" href="../../d5/d33/structcv_1_1HOGDescriptor.html">HOGDescriptor</a> hog;</div><div class="line">    hog.<a class="code" href="../../d5/d33/structcv_1_1HOGDescriptor.html#a75329167dfdacde41b488e091dd555d1">winSize</a> = pos_image_size;</div><div class="line">    hog.<a class="code" href="../../d5/d33/structcv_1_1HOGDescriptor.html#a6de5ac55631eed51e36278cde3a2c159">setSVMDetector</a>( get_svm_detector( svm ) );</div><div class="line">    hog.<a name="a58"></a><a class="code" href="../../d5/d33/structcv_1_1HOGDescriptor.html#a44f2566a83049536a2879f37c977c7fd">save</a>( obj_det_filename );</div><div class="line"></div><div class="line">    test_trained_detector( obj_det_filename, test_dir, videofilename );</div><div class="line"></div><div class="line">    <span class="keywordflow">return</span> 0;</div><div class="line">}</div></div><!-- fragment --> </div><!-- contents -->
<!-- HTML footer for doxygen 1.8.6-->
<!-- start footer part -->
<hr class="footer"/><address class="footer"><small>
Generated on Fri Apr 2 2021 11:36:29 for OpenCV by &#160;<a href="http://www.doxygen.org/index.html">
<img class="footer" src="../../doxygen.png" alt="doxygen"/>
</a> 1.8.13
</small></address>
<script type="text/javascript">
//<![CDATA[
addTutorialsButtons();
//]]>
</script>
</body>
</html>
